package com.dinzeer.legendreliclib.lib.key;

import com.dinzeer.legendreliclib.Legendreliclib;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;
@Mod.EventBusSubscriber
public class SpecialTypeKeyMessage {
    private final int keyIndex;
    private final boolean pressed;
    
    public SpecialTypeKeyMessage(int keyIndex, boolean pressed) {
        this.keyIndex = keyIndex;
        this.pressed = pressed;
    }
    
    public static void encode(SpecialTypeKeyMessage message, FriendlyByteBuf buffer) {
        buffer.writeInt(message.keyIndex);
        buffer.writeBoolean(message.pressed);
    }
    
    public static SpecialTypeKeyMessage decode(FriendlyByteBuf buffer) {
        return new SpecialTypeKeyMessage(buffer.readInt(), buffer.readBoolean());
    }
    
    public static void handle(SpecialTypeKeyMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
        NetworkEvent.Context context = contextSupplier.get();
        context.enqueueWork(() -> {
            ServerPlayer player = context.getSender();
            if (player != null) {
                SpecialKeyType keyType = SpecialKeyType.fromIndex(message.keyIndex);
                
                if (message.pressed) {
                    // 触发按下事件
                    switch (keyType) {
                        case KEY_1 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey1PressedEvent(player));
                        case KEY_2 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey2PressedEvent(player));
                        case KEY_3 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey3PressedEvent(player));
                        case KEY_4 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey4PressedEvent(player));
                        case KEY_5 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey5PressedEvent(player));
                    }
                } else {
                    // 触发松开事件
                    switch (keyType) {
                        case KEY_1 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey1ReleasedEvent(player));
                        case KEY_2 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey2ReleasedEvent(player));
                        case KEY_3 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey3ReleasedEvent(player));
                        case KEY_4 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey4ReleasedEvent(player));
                        case KEY_5 -> MinecraftForge.EVENT_BUS.post(new SpecialTypeKeyEvent.SpecialTypeKey5ReleasedEvent(player));
                    }
                }
            }
        });
        context.setPacketHandled(true);
    }

}